﻿//-----------------------------------------------------------------------
// <copyright file="NewMessagePopupWindow.xaml.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//----------------------------------------------------------------------- 
namespace ChatBox.UI.Windows.NewMessagePopup
{
    using System;
    using System.Windows;
    using System.Windows.Media.Animation;
    using System.Windows.Threading;

    using ChatBox.Utility;

    /// <summary>
    /// Interaction logic for NewMessagePopupWindow.xaml
    /// </summary>
    public partial class NewMessagePopupWindow : Window
    {
        /// <summary>
        /// Timer to close the window
        /// </summary>
        private DispatcherTimer closeTimer;

        /// <summary>
        /// animation to fade in the window
        /// </summary>
        private Storyboard showAnimation;

        /// <summary>
        /// animation to fade out the window
        /// </summary>
        private Storyboard hideAnimation;

        /// <summary>
        /// Initializes a new instance of the NewMessagePopupWindow class
        /// </summary>
        public NewMessagePopupWindow()
        {
            InitializeComponent();

            this.SetUpStoryboards();
            this.StartShow();
            this.StartCloseTimer();
        }

        /// <summary>
        /// Start the show animation
        /// </summary>
        public void StartShow()
        {
            this.showAnimation.Begin();
            this.Show();
        }

        /// <summary>
        /// Set up the animations
        /// </summary>
        private void SetUpStoryboards()
        {
            this.showAnimation = (Storyboard)this.Resources["show"];
            this.hideAnimation = (Storyboard)this.Resources["hide"];
            this.hideAnimation.Completed += this.HideAnimationCompleted;
        }

        /// <summary>
        /// The hide animation completed 
        /// </summary>
        /// <param name="sender">the event sender</param>
        /// <param name="e">the event params</param>
        private void HideAnimationCompleted(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Position the window at the bottom right
        /// </summary>
        /// <param name="currentSize">the windows size</param>
        private void PositionBottomRight(Size currentSize)
        {
            const int Pad = 4;
            var bounds = this.GetScreen().WorkingArea;

            this.Left = bounds.Right - (currentSize.Width + Pad);
            this.Top = bounds.Bottom - (currentSize.Height + Pad);
        }

        /// <summary>
        /// Start the timer that will auto-hide this window
        /// </summary>
        private void StartCloseTimer()
        {
            const int DisplaySeconds = 15;
            this.closeTimer = new DispatcherTimer();
            this.closeTimer.Interval = new TimeSpan(0, 0, DisplaySeconds);
            this.closeTimer.Tick += this.CloseTimerTick;
            this.closeTimer.Start();
        }

        /// <summary>
        /// Auto-close when the timer ticks
        /// </summary>
        /// <param name="sender">the event sender</param>
        /// <param name="e">the event params</param>
        private void CloseTimerTick(object sender, EventArgs e)
        {
            this.closeTimer.Stop();
            this.hideAnimation.Begin();
        }

        /// <summary>
        /// Close button clicked
        /// </summary>
        /// <param name="sender">the event sender</param>
        /// <param name="e">the event params</param>
        private void CloseButtonClick(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Handler for when the window size is set
        /// </summary>
        /// <param name="sender">the event sender</param>
        /// <param name="e">the event params</param>
        private void WindowSizeChanged(object sender, SizeChangedEventArgs e)
        {
            this.PositionBottomRight(e.NewSize);
        }
    }
}
